<?php
class My_address_model extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}
	function getMyAddress($userInfo=null)
	{
		$this->db->select();
		if($userInfo)
		{
			$this->db->where('user_id',$userInfo['id']);
		}
		$this->db->order_by('is_auto', 'desc');
		return $this->db->get('my_address')->result_array();
	}
	function getMyAddressById($id)
	{
		$this->db->select();
		$this->db->where('my_address.id',$id);
		return $this->db->get('my_address')->result_array();
	}
	function addMyAddress($data)
	{
		$data['user_id'] = $this->userInfo['id'];
		$this->db->insert('my_address', $data);
		return $this->db->insert_id('my_address');
	}
	function editMyAddress($data)
	{
		if(isset($data['is_auto']) && $data['is_auto'] == 1)
		{
			$this->db->where('user_id', $this->userInfo['id']);
			$this->db->update('my_address', array('is_auto'=>0));
		}
		$this->db->where('id', $data['id']);
		unset($data['id']);
		$this->db->update('my_address', $data);
	}
	function deleteMyAddress($data)
	{
		echo ($this->db->delete('my_address', array('id' => $data['id'])))?1:0;
	}
}